<template>
	<div class="case">
		<el-dialog v-model="editDialogVisible" :title="title" style="width: 400px;" class='dialog'>
		  <el-form :model="tagSave" label-position='right'>
			<el-form-item label="标签名称">
			  <el-input v-model="tagSave.name" autocomplete="off" placeholder="请输入"/>
			</el-form-item>
		  </el-form>
		  <template #footer>
			<span class="dialog-footer">
			  <el-button @click="editDialogVisible = false">取消</el-button>
			  <el-button type="primary" @click="save">确认</el-button>
			</span>
		  </template>
		</el-dialog>
	  <el-form :inline="true" :model="tagSearch" label-width="70px" style="min-width: 1200px;" class='searchForm'>
		<el-form-item label="标签名称">
			<el-input v-model="tagSearch.name" placeholder="请输入" clearable/>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" @click="reset">重置</el-button>
			<el-button type="primary" @click="search">搜索</el-button>
		</el-form-item>
	  </el-form>
	  <el-table :data="tag_list.results" class='table'>
		<el-table-column label="序号" width="70" type="index"/>
		<el-table-column label="标签名称" prop="name" min-width="100"/>
		<el-table-column label="创建人" prop="create_by_name"  min-width="70"/>
		<el-table-column label="更新人" prop="update_by_name"  min-width="70"/>
		<el-table-column label="创建时间" prop="create_time" sortable min-width="150"/>
		<el-table-column label="更新时间" prop="update_time" sortable min-width="150"/>
		<el-table-column align="center" min-width="270">
			<template #header>
				<el-button v-if='permission.has_add_permission' type='primary' @click="addTag">新增</el-button>
			</template>
			<template #default="scope">
			  <el-button v-if="permission.has_edit_permission" :icon="Edit" size="small" type='primary' @click="editTag(scope.row)">编辑</el-button>
			  <el-button v-if="permission.has_delete_permission"
				@click="deleteTag(scope.row.id)"
				size="small"
				type="danger"
			  :icon="Delete"
				>删除</el-button
			  >
			</template>
		</el-table-column>
	  </el-table>
	  <div style="margin: 10px">
	  	<el-pagination
	  	  v-model:current-page="page_size_params.page"
	  	  v-model:page-size="page_size_params.size"
	  	  :page-sizes="[10, 20, 30, 50]"
	  	  :small="small"
	  	  :disabled="disabled"
	  	  :background="background"
	  	  :hide-on-single-page="false"
	  	  layout="total, sizes, prev, pager, next, jumper"
	  	  :total="tag_list.count"
	  	  @size-change="handleSizeChange"
	  	  @current-change="handleCurrentChange"
	  	/>
	  </div>
	</div>
</template>

<script>
import {mapState, mapActions, mapGetters} from 'vuex'
import { ElMessage, ElMessageBox } from 'element-plus'
import { View, Delete, Edit, Plus, EditPen} from '@element-plus/icons-vue'

export default{
	computed:{
		...mapState(['pathPermission', 'projectInfo']),
	},
	data() {
		return {
			permission: {},
			tagSearch:{
				name: '',
				project: '',
			},
			page_size_params: {
				page: 1,
				size: 10,
			},
			count: 1,
			title: '新增模块',
			isAdd: true,
			editDialogVisible: false,
			tag_list: [],
			service_list: [],
			ViewVisible: false,
			tagSave:{
				project: '',
				name: ''
			},
			role_names: [],
		}
	},
	setup() {
		return {
			Edit,
			Delete,
			Plus,
			View,
			EditPen,
		}
	},
	methods:{
		...mapActions(['getRolePermission']),
		handleCurrentChange(){
			this.getTags()
		},
		handleSizeChange(){
			this.getTags()
		},
		search(){
			this.getTags()
		},
		reset(){
			for(let key in this.tagSearch){
				if(key === 'module_list'){
					this.tagSearch[key] = []
				}else{
					this.tagSearch[key] = ''
				}
			}
		},
		editTag(row_data){
			this.isAdd = false
			this.title = '编辑标签'
			this.getTag(row_data.id)
			this.editDialogVisible = true
		},
		addTag(){
			this.title = '新增标签'
			this.editDialogVisible = true
			this.isAdd = true
		},
		save(){
			this.editDialogVisible = false
			if (this.isAdd){
				this.createTag()
			}else{
				this.updateTag()
			}
		},
		async getTag(id){
			const response = await this.$api.getTag(id)
			if (response.status === 200){
				this.tagSave = response.data.result
			}
		},
		async createTag(params){
			this.tagSave.project = this.projectInfo.id
			const response = await this.$api.createTag(this.tagSave)
			if(response.status === 201){
				this.getTags()
			}
		},
		async updateTag(){
			const response = await this.$api.updateTag(this.tagSave.id, this.tagSave)
			if(response.status === 200){
				this.menuSave = {}
				this.getTags()
			}
		},
		async deleteTag(id){
			const response = await this.$api.deleteTag(id)
			if (response.status === 204){
				this.getTags()
			}
		},
		async getTags(){
			this.tagSearch.project = this.projectInfo.id
			const data = Object.assign(this.tagSearch, this.page_size_params)
			const response = await this.$api.getTags(data)
			if (response.status === 200){
				this.tag_list = {...response.data}
			}
		},
	},
	created() {
		this.getRolePermission(this.pathPermission[this.$route.path]).then(res =>{
			this.permission = {...res.result}
		})
		this.getTags()
	}
}
</script>

<style scoped>
	.case{
		width: 100%;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	.case .searchForm{
		width: 100%;
		flex: 1;
		background-color: white;
		padding: 15px 15px 0 15px;
		margin-bottom: 5px;
	}
	.case .el-form--inline .el-form-item{
		margin-bottom: 15px;
	}
	.case .table{
		background-color: white;
		flex: 1;
	}
	.searchForm .el-input{
		width: 150px;
	}
	.searchForm .el-select{
		width: 150px;
	}
	.form .el-select{
		width: 300px;
	}
	.el-button{
		width: 70px;
	}
	.el-icon{
		margin-left: 10px;
	}
	
</style>
